package com.xinyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinyu.domain.EquipmentInfo;
import com.xinyu.domain.User;
import com.xinyu.mapper.EquipmentInfoMapper;
import com.xinyu.service.IEquipmentInfoService;
import com.xinyu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-17
 */
@Service
public class EquipmentInfoServiceImpl extends ServiceImpl<EquipmentInfoMapper, EquipmentInfo> implements IEquipmentInfoService {

    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Autowired
    private IUserService userService;

    @Override
    public List<EquipmentInfo> queryByUserId(Integer userId) {
        return equipmentInfoMapper.queryByUserId(userId);
    }

    @Override
    public IPage<EquipmentInfo> selectEquipmentInfoPage(Page<EquipmentInfo> page) {
        QueryWrapper<EquipmentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public List<EquipmentInfo> listVisibleMachines(Integer userId) {
        if (userId == null) {
            return Collections.emptyList();
        }
        User user = userService.getById(userId);
        boolean isAdmin = user != null && user.getIsAdmin() != null && user.getIsAdmin() == 1;
        if (isAdmin) {
            return list(new LambdaQueryWrapper<EquipmentInfo>().eq(EquipmentInfo::getIsDelete, 0));
        }
        return equipmentInfoMapper.queryByUserId(userId);
    }
}
